package com.smartstate.EnvironmentMonitor.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;

import com.smartstate.EnvironmentMonitor.dto.AlarmDTO;
import com.smartstate.EnvironmentMonitor.dto.PageDTO;
import com.smartstate.EnvironmentMonitor.entity.Alarm;
import com.smartstate.EnvironmentMonitor.service.impl.AlarmServiceImpl;
import com.smartstate.EnvironmentMonitor.vo.AlarmPageVo;
import com.smartstate.EnvironmentMonitor.vo.AlarmVo;
import com.smartstate.EnvironmentMonitor.service.AlarmService;
import com.smartstate.EnvironmentMonitor.vo.ResultVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import java.util.List;

/**
 * <p>报警信息</p>
 * <p>@Author 陈永杰</p>
 * <p>@Date: 2022/8/16</p>
 * <p>@Time: 10:36</p>
 * <p>@version 1.0 <p>
 */

@RestController
@Api(tags = {"报警信息"})
@RequestMapping("/Alarm")
public class AlarmController {

    @Resource
    private AlarmService alarmService;

    @Autowired
    private AlarmServiceImpl alarmServiceImpl;


    /**
     * 添加报警信息
     *
     * @param
     * @return
     */
    @PostMapping("/addAlarm")
    @ApiOperation(value = "添加报警信息")
    public void addAlarm(Alarm alarm) {
        alarmServiceImpl.setAlarm(alarm);
    }


    @PostMapping("/selectAlarmPage")
    @ApiOperation(value = "搜索框查询 分页")
    public ResultVO selectAlarmPage(@RequestBody PageDTO<AlarmPageVo> pageDTO) {
        return ResultVO.ok(alarmService.queryPageList(pageDTO));
    }


    @PostMapping("/selectAlarm")
    @ApiOperation(value = "搜索框查询")
    public ResultVO selectAlarm(@RequestBody AlarmDTO item) {
        return ResultVO.ok(alarmService.selectAlarm(item));
    }


    @PostMapping("/selectAlarmExcelVo")
    @ApiOperation(value = "导出报警信息excel")
    public ResultVO exportExcel(HttpServletResponse response, @RequestBody AlarmDTO item) throws IOException {
            setExcelRespProp(response, "报警信息列表");
                List<AlarmVo> list = alarmService.selectAlarm(item);
                for (AlarmVo alarmVo:list) {
                    if (alarmVo.getState().equals("1")){
                        alarmVo.setState("已处理");
                    }else if(alarmVo.getState().equals("2")){
                        alarmVo.setState("未处理");
                    }else {
                        alarmVo.setState("处理中");
                    }
                }
                if (list.size()>0){
                    EasyExcel.write(response.getOutputStream())
                            .head(AlarmVo.class)
                            .excelType(ExcelTypeEnum.XLSX)
                            .sheet("用户信息列表")
                            .doWrite(list);
                    return ResultVO.ok();
                }
                return ResultVO.ok("暂无数据");

    }

    @PostMapping("/selectDeviceId")
    @ApiOperation(value = "查询设备属性给到看板")
    public ResultVO selectDeviceId(@RequestBody Alarm alarm) {
        return ResultVO.ok(alarmService.selectDeviceId(alarm.getDeviceId()));
    }


    @PostMapping("/updateDeviceIdTags")
    @ApiOperation(value = "看板修改")
    public ResultVO updateDeviceIdTags(@RequestBody AlarmVo alarm) {
        return ResultVO.ok(alarmService.updateDeviceIdTags(alarm.getDeviceId(),alarm.getTags(),alarm.getWay()));
    }



    /**
     * 设置excel下载响应头属性
     */
    private void setExcelRespProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    }


    @PostMapping("/figureOut")
    @ApiOperation(value = "处理完成")
    public ResultVO<Boolean> figureOut(@RequestBody Alarm alarm) {
        return ResultVO.ok(alarmService.figureOut(alarm));
    }

}